package com.hkbigdata.sybx.mapper;

import com.hkbigdata.sybx.bean.OrderInfo;
import org.apache.ibatis.annotations.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liuanbo
 * @creat 2025-05-28-18:37
 * @see 2194550857@qq.com
 */
@Mapper
public interface OrderMapper {
    /**
     * 插入订单（自动管理时间字段）
     */
    @Insert("INSERT INTO order_info (order_no, user_id, service_type, service_duration, service_price, address, " +
            "visit_time, remarks, order_status, create_time, update_time) " +
            "VALUES (#{orderNo}, #{userId}, #{serviceType}, #{serviceDuration}, #{servicePrice}, #{address}, " +
            "#{visitTime}, #{remarks}, #{orderStatus}, #{createTime}, #{updateTime})")
    int insert(OrderInfo order);

    /**
     * 更新订单状态
     */
    @Update("UPDATE order_info SET order_status = #{orderStatus}, update_time = #{updateTime} WHERE order_no = #{orderNo}")
    int updateStatusByOrderNo(OrderInfo order);

    /**
     * 更新订单信息
     */
    @Update("UPDATE order_info SET " +
            "service_type = #{serviceType}, " +
            "service_duration = #{serviceDuration}, " +
            "service_price = #{servicePrice}, " +
            "address = #{address}, " +
            "visit_time = #{visitTime}, " +
            "remarks = #{remarks}, " +
            "update_time = #{updateTime} " +
            "WHERE order_no = #{orderNo}")
    int updateOrderInfo(OrderInfo order);

    /**
     * 根据订单号查询订单
     */
    @Select("SELECT " +
            "id, " +
            "order_no AS orderNo, " +
            "user_id AS userId, " +
            "service_type AS serviceType, " +
            "service_duration AS serviceDuration, " +
            "service_price AS servicePrice, " +
            "address, " +
            "visit_time AS visitTime, " +
            "remarks, " +
            "order_status AS orderStatus, " +
            "create_time AS createTime, " +
            "update_time AS updateTime " +
            "FROM order_info " +
            "WHERE order_no = #{orderNo}")
    OrderInfo selectByOrderNo(String orderNo);

    /**
     * 根据用户ID查询订单列表，按创建时间降序排列
     */
    @Select("SELECT " +
            "id, " +
            "order_no AS orderNo, " +
            "user_id AS userId, " +
            "service_type AS serviceType, " +
            "service_duration AS serviceDuration, " +
            "service_price AS servicePrice, " +
            "address, " +
            "visit_time AS visitTime, " +
            "remarks, " +
            "order_status AS orderStatus, " +
            "create_time AS createTime, " +
            "update_time AS updateTime " +
            "FROM order_info " +
            "WHERE user_id = #{userId} " +
            "ORDER BY create_time DESC")
    List<OrderInfo> selectByUserId(String userId);

    /**
     * 查询所有订单
     */
    @Select("SELECT " +
            "id, " +
            "order_no AS orderNo, " +
            "user_id AS userId, " +
            "service_type AS serviceType, " +
            "service_duration AS serviceDuration, " +
            "service_price AS servicePrice, " +
            "address, " +
            "visit_time AS visitTime, " +
            "remarks, " +
            "order_status AS orderStatus, " +
            "create_time AS createTime, " +
            "update_time AS updateTime " +
            "FROM order_info " +
            "ORDER BY create_time DESC")
    List<OrderInfo> selectAllOrders();

    /**
     * 删除订单
     */
    @Delete("DELETE FROM order_info WHERE order_no = #{orderNo}")
    int deleteByOrderNo(String orderNo);
}